package a01demo1;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        GirlFriend gf = new GirlFriend();
        while (true) {
            try {
                System.out.println("输入名字");
                String name = sc.nextLine();
                gf.setName(name);

                System.out.println("输入年龄");
                String ages = sc.nextLine();
                int age = Integer.parseInt(ages);
                gf.setAge(age);
                break;

            } catch (NumberFormatException e) {
                System.out.println("年龄格式");
                continue;
            } catch (RuntimeException e) {
                System.out.println("姓名年龄");
                continue;
            }
        }
        System.out.println(gf);

    }
}

class GirlFriend {
    private String name;
    private int age;

    public GirlFriend() {
    }

    public GirlFriend(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        int len = name.length();
        if (len < 3 || len > 10) {
            throw new RuntimeException();
        }
        this.name = name;
    }

    /**
     * 获取
     *
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     *
     * @param age
     */
    public void setAge(int age) {
        if (age < 18 || age > 40) {
            throw new RuntimeException();
        }
        this.age = age;
    }

    public String toString() {
        return "GirlFriend{name = " + name + ", age = " + age + "}";
    }
}